package com.ruoyi.customer.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.customer.mapper.CsOrderPlanMapper;
import com.ruoyi.customer.domain.CsOrderPlan;
import com.ruoyi.customer.service.ICsOrderPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Validator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 客户订单计划Service业务层处理
 * 
 * @author lishouzhuang
 * @date 2025-09-02
 */
@Service
public class CsOrderPlanServiceImpl extends ServiceImpl<CsOrderPlanMapper, CsOrderPlan> implements ICsOrderPlanService
{

    private static final Logger log = LoggerFactory.getLogger(CsOrderPlanServiceImpl.class);

    @Autowired
    protected Validator validator;

    @Autowired
    private CsOrderPlanMapper csOrderPlanMapper;

    /**
     * 查询客户订单计划
     * 
     * @param id 客户订单计划主键
     * @return 客户订单计划
     */
    @Override
    public CsOrderPlan selectCsOrderPlanById(Long id)
    {
        return csOrderPlanMapper.selectCsOrderPlanById(id);
    }

    /**
     * 查询客户订单计划列表
     * 
     * @param csOrderPlan 客户订单计划
     * @return 客户订单计划
     */
    @Override
    public List<CsOrderPlan> selectCsOrderPlanList(CsOrderPlan csOrderPlan)
    {
        return csOrderPlanMapper.selectCsOrderPlanList(csOrderPlan);
    }

    /**
     * 新增客户订单计划
     * 
     * @param csOrderPlan 客户订单计划
     * @return 结果
     */
    @Override
    public int insertCsOrderPlan(CsOrderPlan csOrderPlan)
    {
        csOrderPlan.setCreateTime(DateUtils.getNowDate());
        return csOrderPlanMapper.insertCsOrderPlan(csOrderPlan);
    }

    /**
     * 修改客户订单计划
     * 
     * @param csOrderPlan 客户订单计划
     * @return 结果
     */
    @Override
    public int updateCsOrderPlan(CsOrderPlan csOrderPlan)
    {
        csOrderPlan.setUpdateTime(DateUtils.getNowDate());
        return csOrderPlanMapper.updateCsOrderPlan(csOrderPlan);
    }

    /**
     * 批量删除客户订单计划
     * 
     * @param ids 需要删除的客户订单计划主键
     * @return 结果
     */
    @Override
    public int deleteCsOrderPlanByIds(Long[] ids)
    {
        return csOrderPlanMapper.deleteCsOrderPlanByIds(ids);
    }

    /**
     * 删除客户订单计划信息
     * 
     * @param id 客户订单计划主键
     * @return 结果
     */
    @Override
    public int deleteCsOrderPlanById(Long id)
    {
        return csOrderPlanMapper.deleteCsOrderPlanById(id);
    }






    /**
     * 导入数据
     */
    @Override
    public String importData(List<CsOrderPlan> list, Boolean isUpdateSupport, String operName)
    {
        if (ObjectUtils.isEmpty(list))
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CsOrderPlan record : list)
        {
            try
            {
                // 验证是否存在这个数据
                CsOrderPlan u =  csOrderPlanMapper.selectCsOrderPlanById(record.getId());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, record);
                    this.insertCsOrderPlan(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据id " + record.getId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, record);
                    record.setUpdateBy(operName);
                    this.updateCsOrderPlan(record);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据id " + record.getId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据id " + record.getId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据id " + record.getId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



}
